package yiminsu.com.cn.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yiminsu.com.cn.beans.Homestay;
import yiminsu.com.cn.beans.HomestayImage;
import yiminsu.com.cn.beans.Homestayold;
import yiminsu.com.cn.mapper.HomestayMapper;
import yiminsu.com.cn.service.HomeStayService;
import yiminsu.com.cn.utils.FileUtils;

@Service
public class HomeStayServiceImpl implements HomeStayService{
	
	private static final Logger LOG = Logger.getLogger(HomeStayServiceImpl.class);
	@Autowired
	private HomestayMapper homestayMapper;
	//测试
	@Override
	public Homestayold GetHomestayold(String hotelTitle) {
		
		return homestayMapper.GetHomestayold(hotelTitle);
	}
	//测试
	@Override
	public Homestay GetHomestay(Integer hotelsketchid) {
		
		return homestayMapper.GetHomestay(hotelsketchid);
	}
	//测试
	@Override
	public List<HomestayImage> GetHomestayImage(String qid) {
		
		return homestayMapper.GetHomestayImage(qid);
	}
	//添加新民宿
	@Override
	public Integer Addhomestay(Integer hotetype, String companyname, String hotelTitle, String trafficGuide,
			String provincial, String city, String county, String hotephone, Integer type, List<String> listimage,
			String hotelImage, String hotelVideo, String hotelSynopsis, String hotelIntroduce, String legalperson,
			String legalpersonphone, String legalpersonemail, Double commission, List<String> sfzimage,
			String hotelicense, String companylicense,Integer isconsent,Double longitude,Double latitude,String registerPhoneNum,
			Integer userid,String remarks) {
		LOG.info("开始创建新民宿。。。。。");
		Integer status=null;
	
		//民宿主表实体类
		Homestayold homestayold=new Homestayold();
		homestayold.setHotelIntroduce(hotelIntroduce);//民宿介绍
		homestayold.setHotelTitle(hotelTitle);//民宿名称
		homestayold.setHotelImage(hotelImage);//图片地址
		homestayold.setTrafficGuide(trafficGuide);//民宿地址
		homestayold.setType(type);//风格类型
		homestayold.setLongitude(longitude);//精度
		homestayold.setLatitude(latitude);//纬度
		homestayold.setHotelSynopsis(hotelSynopsis);//民宿标题
		homestayold.setRemarks(remarks);//民宿注意事项
		homestayold.setProvincial(provincial);//省份
		homestayold.setCity(city);//城市
		homestayold.setCounty(county);//补充地址
		homestayold.setPhoneNumber(registerPhoneNum);//联系人电话
		homestayold.setHotelVideo(hotelVideo);//视频地址
		Integer sid=homestayMapper.Getfindaddressid(city);//查询地区id
		Integer fid=(sid!= null)?sid:homestayMapper.Getfindaddressid(provincial);//查询省份id
		homestayold.setPosition(String.valueOf(fid));//设置所属地区id
		homestayold.setUserId(String.valueOf(userid));//民宿主id
		
		try {
			homestayMapper.AddHomestayold(homestayold);
			LOG.error("添加民宿主表成功。。。。。");
		} catch (Exception e) {
			LOG.error("添加民宿主表失败。。。。。");
			status=201;
		}
					
			//民宿副表实体类
			Homestay homestay=new Homestay();
			Homestayold old=homestayMapper.GetHomestayold(hotelTitle);//获取主表民宿
			if(old!=null){
			LOG.info("民宿主表信息表获取成功。。。。。");
			homestay.setHotelsketchid(old.getHotelId());//主表id
			}
			homestay.setHotephone(hotephone);
			homestay.setLegalperson(legalpersonemail);
			homestay.setLegalpersonphone(legalpersonphone);
			homestay.setLegalpersonemail(legalpersonemail);
			homestay.setCommission(commission);
			if(sfzimage.size()==2){
				LOG.info("获取身份证图片地址成功。。。。。");
				homestay.setLegalsfzimgzm(sfzimage.get(0));
				homestay.setLegalsfzimgfm(sfzimage.get(1));
			}else LOG.info("获取身份证图片地址失败。。。。。");
			homestay.setHotelicense(hotelicense);	
			homestay.setCompanyname(companyname);
			homestay.setCompanylicense(companylicense);
			homestay.setHotetype(hotetype);
			homestay.setIsconsent(isconsent);
			
			try {
				homestayMapper.AddHomestay(homestay);
				LOG.error("副表添加成功......");
			
			} catch (Exception e) {
				LOG.error("副表添加失败......");
				status=301;
			}
			LOG.info("开始添加中间表图片。。。。。。");
			for (int i = 0; i < listimage.size(); i++) {
				HomestayImage Image=new HomestayImage();
				Image.setId(FileUtils.GetUUID());
				Image.setQid(String.valueOf(old.getHotelId()));//主表id
				Image.setFilepath(listimage.get(i));
				Image.setFiletype(1);
				Image.setFiletable("hotelsketch");
				Image.setFieldname("hotelImage");
				LOG.info("添加图片中间表第"+i+"次。。。。。。");
				try {
					homestayMapper.AddHomestayImage(Image);
				} catch (Exception e) {
					LOG.error("中间表添加失败......");
					status=401;
				}
				
			}
			status=200;
		return status;
	}
	//删除民宿
	@Override
	public Map<String,Integer> DeleteHomestay(Integer hotelId) {
		Map<String,Integer> map=new HashMap<>();
		try {
			Homestayold home=homestayMapper.GetHomestayoldid(hotelId);
			String []image=home.getHotelImage().split("##");
			Integer str=homestayMapper.DeleteHomestay(hotelId);
			if(str==1){//删除民宿成功
				String dir=System.getProperty("user.dir");		
				for (int i = 0; i < image.length; i++) {
					String path1=dir+"\\src\\main\\resources\\static\\"+image[i].replace("/", "\\");//删除地址
					new File(path1).delete();
				}				
			}
			map.put("status", 200);
		} catch (Exception e) {
			map.put("status", 201);
		}
		
		
		return map;
	}

	
	


}
